﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.Xml;

using $prjsn$.Entity.Services.HowTos;
using $prjsn$.Common;
using $prjsn$.Common.Data.Services.HowTos;

namespace $safeprojectname$.Services.HowTos.Xml
{
    public class SimpleEntityDao : BaseXmlDao<SimpleEntity, string, SimpleEntityFilter>, ISimpleEntityDao
    {
        #region Find
        /// <summary>
        /// Fills the Criteria list with where statements based on provided filter
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override IEnumerable<SimpleEntity> Find(SimpleEntityFilter filter)
        {
            // Equal
            Criteria.Add(entity => filter.IdExact.IsEmpty() || entity.ID.IsExact(filter.IdExact));
            Criteria.Add(entity => entity.NaturalKey.IsExact(filter.NaturalKeySearch)); // exact match even for string.Empty
            Criteria.Add(entity => entity.Description.IsExact(filter.DescriptionSearch));

            // Like
            Criteria.Add(entity => entity.ID.IsLike(filter.IdLike));
            Criteria.Add(entity => entity.NaturalKey.IsLike(filter.NaturalKey));
            Criteria.Add(entity => entity.Description.IsLike(filter.Description));

            // numbers
            Criteria.Add(entity => filter.RealValueFrom.IsNull() || entity.RealValue >= filter.RealValueFrom);
            Criteria.Add(entity => filter.RealValueTo.IsNull() || entity.RealValue <= filter.RealValueTo);

            // bool
            Criteria.Add(entity => filter.IsVisible.IsNull() || entity.IsVisible == filter.IsVisible);

            return GetListByCriteria(filter);
        }
        #endregion Find

        #region XML - ENTITY mapping
        /// <summary>
        /// Converts the XElement into the SimpleEntity
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override SimpleEntity CreateEntity(XElement element)
        {
            var entity = FillEntity(new SimpleEntity(), element); // base converts ValueTypes and String

            // TODO - map other properties (except ValueTypes and string, mapped in the base.FillEntity())

            entity.SetId((string)element.Attribute(Str.Common.ID));
            return entity;
        }
        /// <summary>
        /// Converts the SimpleEntity instnace into the XElement
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        protected override XElement CreateElement(SimpleEntity entity)
        {
            var element = base.CreateElement(entity); // base converts ValueTypes and String

            if (element.Attribute("NewID").Is())
            {
                element.Attribute("NewID").Remove();
            }

            return element;
        }
        /// <summary>
        /// Provide the ID value for newly created Entity instance
        /// </summary>
        /// <returns></returns>
        protected override string GetNextId(SimpleEntity entity = null)
        {
            return entity.ID;
            //throw new NotImplementedException("Not yet implemented"); // TODO remove this and implement
        }
        #endregion XML - ENTITY mapping
    }
}

